﻿using System.Text;

namespace System.Web.Mvc
{

    //public static class TypeExtensions
    //{
    //    public static PropertyInfo[] GetFilteredProperties(this Type type)
    //    {
    //        return type.GetProperties().Where(pi => !Attribute.IsDefined(pi, typeof(SkipPropertyAttribute))).ToArray();
    //    }
    //}




    //public class DoNotIncludeAttribute : Attribute
    //{
    //}

    //public static class ExtensionsOfPropertyInfo
    //{
    //    public static IEnumerable<T> GetAttributes<T>(this PropertyInfo propertyInfo) where T : Attribute
    //    {
    //        return propertyInfo.GetCustomAttributes(typeof(T), true).Cast<T>();
    //    }
    //    public static bool IsMarkedWith<T>(this PropertyInfo propertyInfo) where T : Attribute
    //    {
    //        return property.GetAttributes<T>().Any();
    //    }
    //}


    public static class HtmlExtensions
    {

        public static string Label(this HtmlHelper helper, string target, string text)
        {
            return String.Format("<label for=\"{0}\">{1}</label>", target, text);

        }


        public static MvcHtmlString ImageLink(this HtmlHelper helper, string href, object htmlAttributes, string alternateText, string imageSrc, object imageAttributes)
        {

            const string format = "<a href=\"{0}\" title=\"{3}\" {1}>{2}</a>";
            //const string imageFormat = "<img src=\"{0}\" alt=\"{2}\" {1} />";
            var image = helper.Image(imageSrc, imageAttributes);// string.Format(imageFormat, imageSrc, GetAttributeString(imageAttributes), alternateText);// helper.Image(imageSrc, alternateText, imageAttributes).ToString();

            var sb = string.Format(format, href, GetAttributeString(htmlAttributes), image, alternateText);
            return new MvcHtmlString(sb);
        }



        public static MvcHtmlString Link(this HtmlHelper helper, string href, string linkText, object attributes)
        {

            const string format = "<a href=\"{0}\"{1}>{2}</a>";
            var sb = string.Format(format, href, GetAttributeString(attributes), linkText);
            return new MvcHtmlString(sb);
        }

        public static MvcHtmlString Image(this HtmlHelper helper, string src, object attributes)
        {
            const string format = "<img src=\"{0}\"{1} />";
            var sb = string.Format(format, src, GetAttributeString(attributes));
            return new MvcHtmlString(sb);
        }


        #region Helpers
        private static string GetAttributeString(object htmlAttributes)
        {
            if (htmlAttributes == null)
            {
                return string.Empty;
            }
            const string format = " {0}=\"{1}\"";
            var sb = new StringBuilder();

            var _properties = htmlAttributes.GetType().GetProperties();//.ForEach(p => sb.AppendFormat(format, p.Name, p.Get(htmlAttributes)));

            foreach (var p in _properties)
            {
                object value = p.GetValue(htmlAttributes, null);
                sb.AppendFormat(format, p.Name, value);
            }

            return sb.ToString();
        }
        #endregion

    }
}
